package com.coocaa.typhoon.api.v1.userfile.util;

import com.coocaa.cirrus.common.base.exception.BusinessException;
import com.drew.imaging.FileType;
import com.drew.imaging.FileTypeDetector;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author bijiahao
 * @date : 2025/9/19 23:32
 * @description
 */
@Slf4j
@UtilityClass
public class FileTypeCheckUtil {
    /**
     * 查询的参数
     */
    public static final String IMAGE = "image";
    /**
     * 查询的参数
     */
    private static final String PDF = "pdf";

    // 支持的图片格式集合
    private static final List<String> IMAGE_EXTENSIONS = Arrays.asList("JPG", "JPEG", "PNG", "GIF", "BMP", "WEBP", "TIFF", "SVG");
    private static final List<String> VIDEO_EXTENSIONS = Arrays.asList(
            // 通用网络/移动端
            "MP4", "MOV", "MKV", "AVI", "FLV", "WEBM", "3GP", "3G2", "M4V", "F4V",
            // 流媒体/直播
            "TS", "M3U8",
            // 老格式/广电/DVD
            "WMV", "ASF", "RMVB", "RM", "VOB", "MPG", "MPEG", "M2V", "DAT",
            // 摄像机/高清
            "MTS", "M2TS", "MOD", "TOD",
            // 其他
            "OGV", "DIVX", "XVID", "DV", "MXF"
    );

    private static final List<String> AUDIO_EXTENSIONS = Arrays.asList(
            // 通用压缩/流媒体
            "MP3", "AAC", "M4A", "WMA", "OGG", "OPUS", "WAV", "FLAC", "ALAC",
            // 无损/高清
            "APE", "WV", "WavPack", "DTS", "DSD", "DSF", "DFF",
            // 老格式/移动端
            "AMR", "AWB", "3GA", "RA", "RAM",
            // 广电/碟片
            "AC3", "EC3", "TRUEHD", "MLP", "PCM", "LPCM",
            // 模块/游戏
            "MOD", "XM", "S3M", "IT", "MID", "MIDI", "KAR",
            // 其他
            "AU", "AIFF", "AIF", "AIFC", "VQF", "TTA", "TAK"
    );


    public static boolean isAudioExtension(String fileExtName) {
        return AUDIO_EXTENSIONS.contains(fileExtName.toUpperCase());
    }

    public static boolean isAudioVideo(String fileExtName) {
        return isAudioExtension(fileExtName)
                || isVideoExtension(fileExtName);

    }

    private static final List<String> PPT_EXTENSIONS = Arrays.asList("PPTX", "PPT");

    public static boolean isPDF(String fileExtName) {
        return "PDF".equalsIgnoreCase(fileExtName);
    }

    public static boolean isCanSplitImage(String fileExtName) {
        return isPDF(fileExtName)
                || isPPTExtension(fileExtName)
                || isVideoExtension(fileExtName);
    }

    public static boolean isVideoExtension(String fileExtName) {
        return VIDEO_EXTENSIONS.contains(fileExtName.toUpperCase());
    }

    public static boolean isSupportFileTypeQuery(String fileTypeInput) {
        return IMAGE.equalsIgnoreCase(fileTypeInput)
                || PDF.equalsIgnoreCase(fileTypeInput);
    }

    public static boolean isSupportVikingDb(String fileExtName) {
        return isImageExtension(fileExtName) || isCanSplitImage(fileExtName);
    }

    public static boolean isImageQuery(String fileTypeInput) {
        return IMAGE.equalsIgnoreCase(fileTypeInput);
    }

    public static List<String> getFileTypeForQuery(String fileTypeInput) {
        if (IMAGE.equalsIgnoreCase(fileTypeInput)) {
            return IMAGE_EXTENSIONS.stream().map(String::toUpperCase).collect(Collectors.toList());
        } else if (PDF.equalsIgnoreCase(fileTypeInput)) {
            return Collections.singletonList("PNG");
        }
        return Collections.singletonList(fileTypeInput.toUpperCase());
    }

    public static boolean isImageExtension(String fileExtName) {
        return IMAGE_EXTENSIONS.contains(fileExtName.toUpperCase());
    }

    public static boolean isPPTExtension(String fileType) {
        return PPT_EXTENSIONS.contains(fileType.toUpperCase());
    }

    public static String getFileTypeForSave(String extName) {
        return extName.toUpperCase();
    }


    public static String getFileType(String extName, File outputFile) {
        if (StringUtils.isNotBlank(extName)) {
            return extName.toUpperCase();
        }
        try {
            return FileTypeDetector.detectFileType(new FileInputStream(outputFile)).getName();
        } catch (IOException e) {
            log.error("getFileType", e);
            throw new BusinessException("获取文件类型异常");
        }

    }


}
